package com.zkt.practice.server.config.interceptor;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/24
 * @description Feign请求拦截器
 **/

@Component
public class FeignRequestInterceptor implements RequestInterceptor {
	
	@Override
	public void apply(RequestTemplate requestTemplate) {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		if (ObjUtil.isNotNull(request)) {
			String loginId = request.getHeader("loginId");
			if (StrUtil.isNotBlank(loginId)) {
				requestTemplate.header("loginId", loginId);
			}
		}
	}
	
}
